<?php require_once('../Connections/hitekforyou.php'); ?>
<?php
// Load the common classes
require_once('../includes/common/KT_common.php');

// Load the tNG classes
require_once('../includes/tng/tNG.inc.php');

// Load the KT_back class
require_once('../includes/nxt/KT_back.php');

// Make a transaction dispatcher instance
$tNGs = new tNG_dispatcher("../");

// Make unified connection variable
$conn_hitekforyou = new KT_connection($hitekforyou, $database_hitekforyou);

// Start trigger
$formValidation = new tNG_FormValidation();
$formValidation->addField("idHang", true, "numeric", "", "", "", "Vui lòng Hãng sản xuất");
$formValidation->addField("TenSP", true, "text", "", "", "", "Vui lòng điền đúng dữ liệu cần");
$formValidation->addField("HinhAnh", true, "", "", "", "", "Vui lòng upload hình ảnh cho sản phẩm");
$formValidation->addField("DonGia", true, "numeric", "", "", "", "Vui lòng điền đúng dữ liệu cần");
$formValidation->addField("ThoiGianBaoHanh", true, "numeric", "", "", "", "Vui lòng chọn thời gian bảo hành");
$tNGs->prepareValidation($formValidation);
// End trigger

if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysql_select_db($database_hitekforyou, $hitekforyou);
$query_Recordset3 = "SELECT TenHang, idHang FROM hangsx ORDER BY TenHang";
$Recordset3 = mysql_query($query_Recordset3, $hitekforyou) or die(mysql_error());
$row_Recordset3 = mysql_fetch_assoc($Recordset3);
$totalRows_Recordset3 = mysql_num_rows($Recordset3);

// Make an insert transaction instance
$ins_sanpham = new tNG_multipleInsert($conn_hitekforyou);
$tNGs->addTransaction($ins_sanpham);
// Register triggers
$ins_sanpham->registerTrigger("STARTER", "Trigger_Default_Starter", 1, "POST", "KT_Insert1");
$ins_sanpham->registerTrigger("BEFORE", "Trigger_Default_FormValidation", 10, $formValidation);
$ins_sanpham->registerTrigger("END", "Trigger_Default_Redirect", 99, "../includes/nxt/back.php");
// Add columns
$ins_sanpham->setTable("sanpham");
$ins_sanpham->addColumn("idHang", "NUMERIC_TYPE", "POST", "idHang");
$ins_sanpham->addColumn("TenSP", "STRING_TYPE", "POST", "TenSP");
$ins_sanpham->addColumn("HinhAnh", "FILE_TYPE", "FILES", "HinhAnh");
$ins_sanpham->addColumn("DonGia", "NUMERIC_TYPE", "POST", "DonGia");
$ins_sanpham->addColumn("ThoiGianBaoHanh", "NUMERIC_TYPE", "POST", "ThoiGianBaoHanh");
$ins_sanpham->addColumn("SoLuongTonKho", "NUMERIC_TYPE", "POST", "SoLuongTonKho");
$ins_sanpham->addColumn("AnHien", "CHECKBOX_1_0_TYPE", "POST", "AnHien", "0");
$ins_sanpham->setPrimaryKey("idSP", "NUMERIC_TYPE");

// Make an update transaction instance
$upd_sanpham = new tNG_multipleUpdate($conn_hitekforyou);
$tNGs->addTransaction($upd_sanpham);
// Register triggers
$upd_sanpham->registerTrigger("STARTER", "Trigger_Default_Starter", 1, "POST", "KT_Update1");
$upd_sanpham->registerTrigger("BEFORE", "Trigger_Default_FormValidation", 10, $formValidation);
$upd_sanpham->registerTrigger("END", "Trigger_Default_Redirect", 99, "../includes/nxt/back.php");
// Add columns
$upd_sanpham->setTable("sanpham");
$upd_sanpham->addColumn("idHang", "NUMERIC_TYPE", "POST", "idHang");
$upd_sanpham->addColumn("TenSP", "STRING_TYPE", "POST", "TenSP");
$upd_sanpham->addColumn("HinhAnh", "FILE_TYPE", "FILES", "HinhAnh");
$upd_sanpham->addColumn("DonGia", "NUMERIC_TYPE", "POST", "DonGia");
$upd_sanpham->addColumn("ThoiGianBaoHanh", "NUMERIC_TYPE", "POST", "ThoiGianBaoHanh");
$upd_sanpham->addColumn("SoLuongTonKho", "NUMERIC_TYPE", "POST", "SoLuongTonKho");
$upd_sanpham->addColumn("AnHien", "CHECKBOX_1_0_TYPE", "POST", "AnHien");
$upd_sanpham->setPrimaryKey("idSP", "NUMERIC_TYPE", "GET", "idSP");

// Make an instance of the transaction object
$del_sanpham = new tNG_multipleDelete($conn_hitekforyou);
$tNGs->addTransaction($del_sanpham);
// Register triggers
$del_sanpham->registerTrigger("STARTER", "Trigger_Default_Starter", 1, "POST", "KT_Delete1");
$del_sanpham->registerTrigger("END", "Trigger_Default_Redirect", 99, "../includes/nxt/back.php");
// Add columns
$del_sanpham->setTable("sanpham");
$del_sanpham->setPrimaryKey("idSP", "NUMERIC_TYPE", "GET", "idSP");

// Execute all the registered transactions
$tNGs->executeTransactions();

// Get the transaction recordset
$rssanpham = $tNGs->getRecordset("sanpham");
$row_rssanpham = mysql_fetch_assoc($rssanpham);
$totalRows_rssanpham = mysql_num_rows($rssanpham);


error_reporting(0);
require_once('checklogin.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<link href="quantri.css" rel="stylesheet" type="text/css" />
<script src="../AC_RunActiveContent.js" type="text/javascript"></script>

<title>Trang chủ Quản trị</title>
<link href="../includes/skins/mxkollection3.css" rel="stylesheet" type="text/css" media="all" />
<script src="../includes/common/js/base.js" type="text/javascript"></script>
<script src="../includes/common/js/utility.js" type="text/javascript"></script>
<script src="../includes/skins/style.js" type="text/javascript"></script><?php echo $tNGs->displayValidationRules();?><script src="../includes/nxt/scripts/form.js" type="text/javascript"></script><script src="../includes/nxt/scripts/form.js.php" type="text/javascript"></script><script type="text/javascript">
$NXT_FORM_SETTINGS = {
  duplicate_buttons: true,
  show_as_grid: true,
  merge_down_value: true
}
</script>


</head>

<body>

<?php include("admin_template/header.php"); ?>
<?php include("admin_template/admin_banner.php"); ?>  
<table id="main" width="990" border="1" align="center" cellpadding="0" cellspacing="0">
 <tr>
    <td width="200" align="left" valign="top" bgcolor="#66CC00" id="cottrai">
      <div align="left">
      <?php include("menu.php"); ?>  
      </div>       </td>
    <td id="cotphai" width="790" align="left" valign="top" bgcolor="#FFFFFF">
    
<?php
	echo $tNGs->getErrorMsg();
?>
<div class="KT_tng">
  <h1>
    <?php 
// Show IF Conditional region1 
if (@$_GET['idSP'] == "") {
?>
Thêm
    <?php 
// else Conditional region1
} else { ?>
    Cập nhật
    <?php } 
// endif Conditional region1
?>
    Sản phẩm </h1>
  <div class="KT_tngform">
    <form method="post" id="form1" action="<?php echo KT_escapeAttribute(KT_getFullUri()); ?>" enctype="multipart/form-data">
      <?php $cnt1 = 0; ?>
      <?php do { ?>
      <?php $cnt1++; ?>
      <?php 
// Show IF Conditional region1 
if (@$totalRows_rssanpham > 1) {
?>
      <h2><?php echo NXT_getResource("Record_FH"); ?> <?php echo $cnt1; ?></h2>
      <?php } 
// endif Conditional region1
?>
      <table cellpadding="2" cellspacing="0" class="KT_tngtable">
        <tr>
          <td class="KT_th"><label for="idHang_<?php echo $cnt1; ?>">ID Nhà cung ứng:</label></td>
          <td><select name="idHang_<?php echo $cnt1; ?>" id="idHang_<?php echo $cnt1; ?>">

            <?php 
do {  
?>
            <option value="<?php echo $row_Recordset3['idHang']?>"<?php if (!(strcmp($row_Recordset3['idHang'], $row_rssanpham['idHang']))) {echo "SELECTED";} ?>><?php echo $row_Recordset3['TenHang']?></option>
            <?php
} while ($row_Recordset3 = mysql_fetch_assoc($Recordset3));
  $rows = mysql_num_rows($Recordset3);
  if($rows > 0) {
      mysql_data_seek($Recordset3, 0);
	  $row_Recordset3 = mysql_fetch_assoc($Recordset3);
  }
?>
          </select>
              <?php echo $tNGs->displayFieldError("sanpham", "idHang", $cnt1); ?> </td>
        </tr>
        <tr>
          <td class="KT_th"><label for="TenSP_<?php echo $cnt1; ?>">Tên sản phẩm:</label></td>
          <td><input type="text" name="TenSP_<?php echo $cnt1; ?>" id="TenSP_<?php echo $cnt1; ?>" value="<?php echo KT_escapeAttribute($row_rssanpham['TenSP']); ?>" size="32" maxlength="255" />
              <?php echo $tNGs->displayFieldHint("TenSP");?> <?php echo $tNGs->displayFieldError("sanpham", "TenSP", $cnt1); ?> </td>
        </tr>
        <tr>
          <td class="KT_th"><label for="HinhAnh_<?php echo $cnt1; ?>">Hình ảnh:</label></td>
          <td><input type="file" name="HinhAnh_<?php echo $cnt1; ?>" id="HinhAnh_<?php echo $cnt1; ?>" size="32" />
              <?php echo $tNGs->displayFieldError("sanpham", "HinhAnh", $cnt1); ?> </td>
        </tr>
        <tr>
          <td class="KT_th"><label for="DonGia_<?php echo $cnt1; ?>">DonGia:</label></td>
          <td><input type="text" name="DonGia_<?php echo $cnt1; ?>" id="DonGia_<?php echo $cnt1; ?>" value="<?php echo KT_escapeAttribute($row_rssanpham['DonGia']); ?>" size="11" />
              <?php echo $tNGs->displayFieldHint("DonGia");?> <?php echo $tNGs->displayFieldError("sanpham", "DonGia", $cnt1); ?> </td>
        </tr>
        <tr>
          <td class="KT_th"><label for="ThoiGianBaoHanh_<?php echo $cnt1; ?>">ThoiGianBaoHanh:</label></td>
          <td><select name="ThoiGianBaoHanh_<?php echo $cnt1; ?>" id="ThoiGianBaoHanh_<?php echo $cnt1; ?>">
            <option value="12" <?php if (!(strcmp(12, KT_escapeAttribute($row_rssanpham['ThoiGianBaoHanh'])))) {echo "SELECTED";} ?>>12 Tháng</option>
            <option value="24" <?php if (!(strcmp(24, KT_escapeAttribute($row_rssanpham['ThoiGianBaoHanh'])))) {echo "SELECTED";} ?>>24 Tháng</option>
            <option value="36" <?php if (!(strcmp(36, KT_escapeAttribute($row_rssanpham['ThoiGianBaoHanh'])))) {echo "SELECTED";} ?>>36 Tháng</option>
          </select>
              <?php echo $tNGs->displayFieldError("sanpham", "ThoiGianBaoHanh", $cnt1); ?> </td>
        </tr>
        <tr>
          <td class="KT_th"><label for="SoLuongTonKho_<?php echo $cnt1; ?>">SoLuongTonKho:</label></td>
          <td><input type="text" name="SoLuongTonKho_<?php echo $cnt1; ?>" id="SoLuongTonKho_<?php echo $cnt1; ?>" value="<?php echo KT_escapeAttribute($row_rssanpham['SoLuongTonKho']); ?>" size="11" />
              <?php echo $tNGs->displayFieldHint("SoLuongTonKho");?> <?php echo $tNGs->displayFieldError("sanpham", "SoLuongTonKho", $cnt1); ?> </td>
        </tr>
        <tr>
          <td class="KT_th"><label for="AnHien_<?php echo $cnt1; ?>">Ẩn-Hiện:</label></td>
          <td><input  <?php if (!(strcmp(KT_escapeAttribute($row_rssanpham['AnHien']),"1"))) {echo "checked";} ?> type="checkbox" name="AnHien_<?php echo $cnt1; ?>" id="AnHien_<?php echo $cnt1; ?>" value="1" />
              <?php echo $tNGs->displayFieldError("sanpham", "AnHien", $cnt1); ?> </td>
        </tr>
      </table>
      <input type="hidden" name="kt_pk_sanpham_<?php echo $cnt1; ?>" class="id_field" value="<?php echo KT_escapeAttribute($row_rssanpham['kt_pk_sanpham']); ?>" />
      <?php } while ($row_rssanpham = mysql_fetch_assoc($rssanpham)); ?>
      <div class="KT_bottombuttons">
        <div>
          <?php 
      // Show IF Conditional region1
      if (@$_GET['idSP'] == "") {
      ?>
          <input type="submit" name="KT_Insert1" id="KT_Insert1" value="Thêm" />
          <?php 
      // else Conditional region1
      } else { ?>
          <div class="KT_operations">
            <input type="submit" name="KT_Insert1" value="Thêm mới" onclick="nxt_form_insertasnew(this, 'idSP')" />
          </div>
          <input type="submit" name="KT_Update1" value="Thêm" />
          <input type="submit" name="KT_Delete1" value="Xóa" onclick="return confirm('<?php echo NXT_getResource("Bạn có chắc muốn xóa ?"); ?>');" />
          <?php }
      // endif Conditional region1
      ?>
          <input type="button" name="KT_Cancel1" value="Hủy" onclick="return UNI_navigateCancel(event, '../includes/nxt/back.php')" />
        </div>
      </div>
    </form>
  </div>
  <br class="clearfixplain" />
</div>
<p>&nbsp;</p>

 </tr>
</table>

<?php include("admin_template/footer.php"); ?>  
</body>
</html>
<?php
mysql_free_result($Recordset3);
?>
